// export const URL = "https://xcx.diaochan88.com/technicianapp/";
// export const URL = "https://app.yuyuedaojia6.com/technicianapp/";
import { config } from "../common/config";

function isString(value) {
  return Object.prototype.toString.call(value) === "[object String]";
}

export const request = (options) => {
  const token = uni.getStorageSync("token");
  if (token) {
    options.header = {
      ...options.header,
      token: `${token}`,
    };
  }
  return new Promise((resolve, reject) => {
    uni.request({
      url: config.URL + options.url,
      method: options.method || "GET",
      data: options.data || {},
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        ...options.header,
      },
      success: (res) => {
        // console.log("-->>", res);
        if (res.data.status === 200 || res.statusCode === 200) {
          if (isString(res.data) && res.data.indexOf("Invalid") !== -1) {
            reject(res.data);
          }
          resolve(res.data);
        } else if (res.statusCode === 500) {
          reject(res.data);
        }
      },
      fail: (err) => {
        console.log("err", err);

        reject(err);
      },
    });
  });
};
